package com.nexgo.payment.trans.setting.terminalkey;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputFilter;
import android.view.View;
import android.widget.EditText;

import com.nexgo.payment.R;
import com.nexgo.payment.adapter.ListMenuAdapter;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.trans.setting.SaveParameterActivity;
import com.nexgo.payment.trans.setting.SaveParameterPresenter;
import com.nexgo.payment.trans.setting.terminalkey.dowmtmk.TmkDownloadActivity;
import com.nexgo.payment.util.StringUtils;
import com.nexgo.payment.util.UIUtils;
import com.nexgo.payment.widget.TitleBuilder;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 终端密钥值设置
 */
public class SetupMasterKeyValueActivity extends SaveParameterActivity implements ListMenuAdapter.OnItemClickListener {

    @BindView(R.id.master_key_index)
    EditText masterKeyIndex;
    @BindView(R.id.master_key_value)
    EditText masterKeyValue;
    @BindView(R.id.list_menu)
    RecyclerView mListMenu;

    private ListMenuAdapter mAdapter;
    public static final int[] ATRRS = new int[]{
            android.R.attr.listDivider
    };
    private Drawable mDivider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup_master_key_value);
        ButterKnife.bind(this);
        new SaveParameterPresenter(this, Repository.getInstance());

        initTitle();
        initView();
    }

    //初始化标题栏
    private void initTitle() {
        new TitleBuilder(this)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setLeftImage(R.drawable.arrow_icon)
                .setRightText(getString(R.string.action_save))
                .setTitleText(getString(R.string.setup_master_key_value))
                .build();
    }

    /**
     * 初始化视图
     */
    private void initView(){
        final TypedArray ta = obtainStyledAttributes(ATRRS);
        mDivider = ta.getDrawable(0);
        ta.recycle();
        LinearLayoutManager llm = new LinearLayoutManager(this);
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        mListMenu.setLayoutManager(llm);
        mListMenu.setHasFixedSize(true);
        mAdapter = new ListMenuAdapter();
        mAdapter.setOnItemClickListener(this);
        mListMenu.setAdapter(mAdapter);
        mListMenu.addItemDecoration(new RecyclerView.ItemDecoration() {

            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                int left = parent.getPaddingLeft();
                int right = parent.getWidth() - parent.getPaddingRight();
                final int childCount = parent.getChildCount();
                for (int i = 0; i < childCount; i++) {
                    final View child = parent.getChildAt(i);

                    //获得child的布局信息
                    final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
                    final int top = child.getBottom() + params.bottomMargin;
                    final int bottom = top + mDivider.getIntrinsicHeight();
                    mDivider.setBounds(left, top, right, bottom);
                    mDivider.draw(c);
                }
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
            }
        });
        //菜单项
//        mAdapter.addItem(R.string.import_masker_key,0);
//        mAdapter.addItem(R.string.manual_cipher_tmk,0);
//        mAdapter.addItem(R.string.ic_card_import_key,0);
        //wzp add
//        mAdapter.addItem(R.string.download_master_key,0);
        mAdapter.addItem(R.string.download_master_key_new,0);
    }

    @Override
    protected HashMap<String, String> getDataBind() {
        HashMap<String, String> map = new HashMap<>();
        map.put(PreferencesConstants.INPUT_MASTER_KEY_INDEX, masterKeyIndex.getText().toString());
        map.put(PreferencesConstants.INPUT_MASTER_KEY_VALUE, masterKeyValue.getText().toString());
        return map;
    }

    @Override
    public void showDetail() {
        //初始化数据
        masterKeyIndex.setText(StringUtils.firstNonEmpty(mPresenter.getParamValue(PreferencesConstants.MASTER_KEY_INDEX), "0"));
        if ("0".equals(mPresenter.getParamValue(PreferencesConstants.MASTER_KEY_ALGORITHM))) {
            //单des
            masterKeyValue.setFilters(new InputFilter[]{new InputFilter.LengthFilter(16)});
        }
    }

    @OnClick({R.id.title_iv_left, R.id.title_tv_right})
    public void onClicked(View view) {
        if (UIUtils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.title_iv_left:
                finish();
                break;
            case R.id.title_tv_right:
                mPresenter.saveParams(getDataBind());
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(int position) {
        if (UIUtils.isDoubleClick()) {
            return;
        }
        switch (position){

 /*           case 0:
                intent2Activity(DownloadKeyActivity.class);
                break;
             case 1:
                intent2Activity(ManualTmkActivity.class);
                break;
                case 2:
                intent2Activity(ICCardImportKeyActivity.class);
                break;*/
            /*case 0:  //wzp add
                intent2Activity(BccDownloadKeyActivity.class);
                break;*/
            case 0:  //wzp add
                intent2Activity(TmkDownloadActivity.class);
                break;
            default:
                break;
        }
    }

    public static Intent newInstance(Context context) {
        return new Intent(context, SetupMasterKeyValueActivity.class);
    }
}
